<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\DAO;

import("Applications.AlloMediaPortal.Classes.Model.AlloMediaUser");
import("Applications.DefaultApp.Classes.DAO.UserDAO");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Database\Builder\QueryBuilder;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\AlloMediaUser;
use \PsiFramework\Applications\DefaultApp\Classes\DAO\UserDAO;

/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The AlloMediaUserDAO is the Data Access Object for the class AlloMediaUser
 *
 * @package DAO
 */
class AlloMediaUserDAO extends UserDAO
{

    protected static $_singleton = null;

    public function delete(
        Object &$object,
        /*bool*/ $performTransaction=true,
        /*bool*/ $checkSecurity=true
    )
    {

        $userRoleDao = DAOFactory::getDAO('SecurityUserRole');
        $alloMediaEntryDao = DAOFactory::getDAO('AlloMediaEntry');

        if ($performTransaction) {
        	$this->_database->startTransaction();
        }

        $ok = true ;
        $entries = $object->getEntries();
        foreach ($entries as $entry) {
            $entry->setUser(null);
            $ok = $alloMediaEntryDao->save($entry, false, $checkSecurity);
            if (!$ok) {
            	break;
            }
        }

        if ($ok) {
            $roles = $object->getRoles();
            foreach ($roles as $userRole) {
                $ok = $userRoleDao->delete($userRole, false, $checkSecurity);
                if (!$ok) {
                	break;
                }
            }
        }

        if ($ok) {
        	$ok = parent::delete($object, false, $checkSecurity);
        }

        if ($performTransaction) {
            if ($ok) {
                $this->_database->commit();
            } else {
                $this->_database->rollback();
            }
        }

        return $ok ;
    }

    /**
     * Retrieve the user who corresponds to the specified login and password
     *
     * @param     string    $login        the user login
     * @param     string    $password    the user password
     * @return     User    the retrieved user
     */
    public function retrieveByLoginPassword(
        /*string*/ $login,
        /*string*/ $password
    )
    {

        $queryBuilder = new QueryBuilder();
        $queryBuilder->createSelectQuery();

        $table = new SqlTable("users", "psiframework");
        $queryBuilder->setTable($table);

        $columnlogin = new SqlColumn("user_login", $table);
        $columnEmail = new SqlColumn("user_email", $table);
        $columnPassword = new SqlColumn("user_password", $table);

        $or = new SqlOperation(SqlOperation::$or);

        $equalLogin = new SqlOperation(SqlOperation::$equal);
        $equalLogin->addParameter($columnlogin);
        $equalLogin->addParameter(new SqlValue($login));

        $equalEmail = new SqlOperation(SqlOperation::$equal);
        $equalEmail->addParameter($columnEmail);
        $equalEmail->addParameter(new SqlValue($login));

        $or->addParameter($equalLogin);
        $or->addParameter($equalEmail);

        $equalPassword = new SqlOperation(SqlOperation::$equal);
        $equalPassword->addParameter($columnPassword);
        $equalPassword->addParameter(new SqlValue($password));

        $and = new SqlOperation(SqlOperation::$and);

        $and->addParameter($or);
        $and->addParameter($equalPassword);

        $queryBuilder->addWhereClause($and);

        $query = $queryBuilder->buildQuery();

        $result = $this->_database->executeQuery($query);
        $user = null ;
        if ($result) {
            $obj = $this->_database->getNextResult($result);
            if ($obj) {
                $user = self::getObjectFromSqlObject($obj);
            }
        }
        return $user;
    }

    public function retrieveByPartialName(/*string*/ $partialName)
    {

        $colNom = new SqlColumn("user_login");
        $value = '%' ;
        if (!isNullOrEmptyString($partialName)) {
            $value .= mb_strtoupper($partialName, 'utf-8') . "%" ;
        }
        $sqlValue = new SqlValue($value);

        $upper = new SqlFunction("UPPER");
        $upper->addParameter($colNom);

        $like = new SqlOperation(SqlOperation::$like);
        $like->addParameter($upper);
        $like->addParameter($sqlValue);

        return $this->retrieveAll(new QueryFilter($like));

    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return AlloMediaUserDAO    instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new AlloMediaUserDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
